package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-21
 */
//课程管理--添加课程--填写课程基本信息控制器
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService service;

    //课程列表
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list=service.list(null);
        return R.ok().data("list",list);
    }

    //条件查询带分页
    @PostMapping("pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current,@PathVariable long limit,
                            @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> page=new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
        String name=courseQuery.getName();
        String status=courseQuery.getStatus();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("title",name);//模糊查询
        }
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        queryWrapper.orderByDesc("gmt_create");
        service.page(page,queryWrapper);

        //获得该页的总记录
        List<EduCourse> courses=page.getRecords();
        //获得总记录数
        long nums=page.getTotal();
        return R.ok().data("data",courses).data("total",nums);
    }


    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id=service.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=service.getCourseId(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        service.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=service.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        service.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        service.removeCourse(courseId);
        return R.ok();
    }
}

